package com.anjubao.parking.municipal.payment.service;

import com.anjubao.parking.municipal.common.exception.ServiceException;
import com.anjubao.parking.municipal.common.utils.MunicipalParkingUtils;
import com.anjubao.parking.municipal.common.utils.U;
import com.anjubao.parking.municipal.payment.constant.PayChannelIdEnum;
import com.anjubao.parking.municipal.payment.constant.TradeTypeEnum;
import com.anjubao.parking.municipal.payment.form.WxPayJsapiForm;
import com.anjubao.parking.municipal.payment.mapper.ParkingTradePayRequestMapper;
import com.anjubao.parking.municipal.payment.model.ParkingTradePayRequest;
import com.github.binarywang.wxpay.bean.WxPayApiData;
import com.github.binarywang.wxpay.bean.order.WxPayMpOrderResult;
import com.github.binarywang.wxpay.bean.order.WxPayNativeOrderResult;
import com.github.binarywang.wxpay.bean.request.WxPayUnifiedOrderRequest;
import com.github.binarywang.wxpay.exception.WxPayException;
import com.github.binarywang.wxpay.service.WxPayService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;

/**
 * @author zhuchunfeng
 * @date 2019/4/26 15:42
 */
@Service
@Slf4j
public class WeiXinPayService {

    @Autowired
    private WxPayService wxPayService;
    @Resource
    private ParkingTradePayRequestMapper parkingTradePayRequestMapper;


    public WxPayMpOrderResult wxPayJSAPI(WxPayJsapiForm payRequestDto) {
        return this.weiXinPayOrder(payRequestDto, TradeTypeEnum.JSAPI);
    }

    public WxPayNativeOrderResult wxPayNative(WxPayJsapiForm payRequestDto) {
        return this.weiXinPayOrder(payRequestDto, TradeTypeEnum.NATIVE);
    }

    private <T> T weiXinPayOrder(WxPayJsapiForm payRequestDto, TradeTypeEnum tradeTypeEnum) {

        /*构建微信支付请求参数*/
        WxPayUnifiedOrderRequest request = new WxPayUnifiedOrderRequest();
        request.setBody("城市智慧停车订单");
//        request.setTotalFee(payRequestDto.getTotalFee());
        request.setTotalFee(1);
        request.setSpbillCreateIp("127.0.0.1");
        request.setOpenid(payRequestDto.getOpenid());
        request.setOutTradeNo(payRequestDto.getOutTradeNo());
//        request.setNotifyUrl(); //支付回调地址 配置在nacos
        request.setTradeType(tradeTypeEnum.getCode());
        request.setAttach(payRequestDto.getAttach());
        if (TradeTypeEnum.NATIVE.getCode().equals(tradeTypeEnum.getCode())) {
            request.setProductId("1"); //native支付productId为必传项
        }
        try {
            //调用统一下单接口，并组装生成支付所需参数对象.
            T order = wxPayService.createOrder(request);
            //获取支付参数
            WxPayApiData wxApiData = wxPayService.getWxApiData();
            //支付参数入库
            ParkingTradePayRequest payRequest = new ParkingTradePayRequest();
            payRequest.setId(U.id());
            payRequest.setOutTradeId(MunicipalParkingUtils.getIdByCode(payRequestDto.getOutTradeNo()));
            payRequest.setPayChannelId(PayChannelIdEnum.WX_PAY.getCode());
            payRequest.setParams(wxApiData.getRequestData());
            payRequest.setChargeRequest(wxApiData.getUrl());
            payRequest.setCreateTime(LocalDateTime.now());
            parkingTradePayRequestMapper.insert(payRequest);
            return order;
        } catch (WxPayException e) {
            wxPayService.getWxApiData();
            log.error("微信支付发起失败 错误信息 {}", e.getMessage());
            throw new ServiceException(e.getMessage());
        }
    }

}
